package tingche;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

import org.junit.jupiter.api.Test;

public class tingche {
	private static List<CarTingBean> carTingList = new ArrayList<CarTingBean>();
	private static List<ShouFeiBean> shouFeiList = new ArrayList<ShouFeiBean>();

	public static void main(String[] args) {
		while (true) {
			System.out.println("0.描述停车场多少车位");
			// 进车 输入车牌号 当前时间 判断车辆高度低于3米可以进入 否则结束
			System.out.println("1.车辆入位");
			// 停车位状态 车牌、开始时间、结束时间、产生费用(计算)
			System.out.println("2.查询所有停车位的状态");
			// 按车牌及停车位号取车，取车时收停车费
			System.out.println("3.按车牌以及停车位号取车");
			// 查询统计全部的收费记录(车牌、开始时间、结束时间、产生的费用)
			System.out.println("4.查询统计全部的收费记录");
			System.out.println("先执行0操作:");
			tingche t = new tingche();
			t.caozuo();
		}
	}

	public void caozuo() {
		Scanner sc = new Scanner(System.in);
		String caozuo = sc.nextLine();
		switch (caozuo) {
		case "0":
			cheWei();
			break;
		case "1":
			jinChe();
			break;
		case "2":
			lookTingChe();
			break;
		case "3":
			selectByPaiTing();
			break;
		case "4":
			selectMoney();
			break;

		default:
			break;
		}
	}

	public void cheWei() {
		System.out.println("输入有多少车位");
		Scanner sc = new Scanner(System.in);
		Integer n = Integer.parseInt(sc.nextLine());
		for (int i = 0; i < n; i++) {
			CarTingBean carTing=new CarTingBean();
			carTing.setId(i);
			carTing.setStat(0);
			carTingList.add(carTing);
		}
	}

	@Test
	public void jinChe() {
		
		Scanner sc = new Scanner(System.in);
		System.out.println("确定停车位号");
		Integer tingchewei = Integer.parseInt(sc.nextLine());
		System.out.println("扫描车牌号");
		String chepaihao = sc.nextLine();

//		System.out.println("结束时间(请按格式0000-00-00 00-00-00)");
//		String endtime = sc.nextLine();

		System.out.println("测量车辆高度");
		Integer gaodu = Integer.parseInt(sc.nextLine());

		// Calendar获取开始时间
		Calendar start = Calendar.getInstance();
		Date time = start.getTime();
		
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
		String timefmt = fmt.format(time);
		
		CarTingBean carting = null;
		int statu=0;
		
		for (int i = 0; i < carTingList.size() ; i++) {
			carting=carTingList.get(i);
			if (carting.getId()==tingchewei) {
				statu=carting.getStat();
				break;
			}
		}
		//1—高度不符合 2-停车位已占用
		if (statu==1) {
			System.out.println("车位已占用");
			return;
		}
		if (gaodu>3) {
			System.out.println("车辆高度不符合要求");
			return;
		}
		if(statu==0&&gaodu<3) {
			System.out.println("车辆入场");
		}
		
		//需要carTing类状态修改
		carting.setStat(1);
		ShouFeiBean shoufei=new ShouFeiBean();
		shoufei.setCheWeiHao(tingchewei);
		shoufei.setChePai(chepaihao);
		shoufei.setStartTime(timefmt);
		shouFeiList.add(shoufei);
		
		System.out.println("全部停车信息");
		for (ShouFeiBean shoufei1 : shouFeiList) {
			System.out.println(shoufei1.toString());
		}
		//测试
//		System.out.println("————");
//		System.out.println(carTingList.get(0).toString());

	}

	public void lookTingChe() {
		
		for (int i = 0; i < carTingList.size(); i++) {
			System.out.println(carTingList.get(i).toString());
		}
	}

	public void selectByPaiTing() {
		
		Scanner sc=new Scanner(System.in);
		System.out.println("请输入你的车牌");
		String chepai = sc.nextLine();
		System.out.println("请输入你的停车位号");
		Integer tingNum= Integer.parseInt(sc.nextLine());
		
		SimpleDateFormat fmt=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
		//结束时间
		Calendar calend=Calendar.getInstance();
		long endTime=calend.getTimeInMillis();
		String end=fmt.format(calend.getTime());
		
		//开始时间
		Calendar calstart=Calendar.getInstance();
		
		
		for (int i = 0; i < shouFeiList.size(); i++) {
			ShouFeiBean shoufei = shouFeiList.get(i);
			Integer id = shoufei.getCheWeiHao();
			String chepai2 = shoufei.getChePai();
			//车牌和停车位号取车，收取停车费
			if (id.equals(tingNum)&&chepai2.equals(chepai)) {
				
				String start = shoufei.getStartTime();
//				Date startTime=new Date(start);
				try {
					//Date类型赋值给Calendar类型，获取毫秒
					Date startTime = fmt.parse(start);
					calstart.setTime(startTime);
					//计算收费
					//获取开始时间毫秒
					long sta = calstart.getTimeInMillis();
//					System.out.println(sta);
					long hour=(endTime-sta)/1000/60/60;
//					System.out.println(hour);
					
//					System.out.println("——————————————");
//					System.out.println(h);
					
					int round = Math.round(hour);
					
//					System.out.println("——————————————");
//					System.out.println(round);
					//测试的时候间隔时间太短,也需要按一小时算
					if (round==0) {
						round=1;
					}
					Double Money=Double.valueOf(round*2);
					shoufei.setMoney(Money);
					shoufei.setEndTime(end);
					System.out.println("收取费用："+Money);
					break;
				} catch (ParseException e) {
					e.printStackTrace();
				}
				
//				carTingList.remove(i);
			}
				System.out.println("你输入的车牌或停车位号不正确");
			
		}
		
		
		
	}
	@Test
	public void selectMoney() {
		System.out.println("全部收费记录：");
		for (int i = 0; i < shouFeiList.size(); i++) {
			ShouFeiBean shouFeiBean = shouFeiList.get(i);
			System.out.println(shouFeiBean.toString());
		}
	}
}
